package configuration

import (
	"administrator/app/apputils/tokenutils"
	"administrator/app/mapper"
	"administrator/app/model"
	"errors"
	"gitee.com/aurora-engine/utils/rep"
	"gitee.com/aurora-engine/web"
)

type FuncManagement struct {
	*mapper.FuncManageMapper
}

func NewFuncManagement() web.Constructor {
	return func() any {
		return &FuncManagement{}
	}
}

// FuncList
// @Description :
// [Get] => /api/configuration/func/list
func (manage *FuncManagement) FuncList() (rep.Response, error) {
	var err error
	var list []*model.FuncRouter
	if list, err = manage.SelectAllFunc(); err != nil {
		return rep.Fail(err, "功能列表查询失败"), err
	}
	return rep.Success(list), nil
}

// FuncRoleList
// @Description :
// @Param args :
// [Get] => /api/configuration/func/role/list
func (manage *FuncManagement) FuncRoleList(args map[string]any, token *tokenutils.Token) (rep.Response, error) {
	var err error
	var list []*model.FuncRouter
	args["userId"] = token.ID
	if list, err = manage.SelectRoleFunc(args); err != nil {
		return rep.Fail(err, "查询失败"), err
	}
	return rep.Success(list), nil
}

// Add
// @Description :
// @Param args :
// [Post] => /api/configuration/func/add
func (manage *FuncManagement) Add(args map[string]any) (rep.Response, error) {
	var err error
	if err = manage.AddFunc(args); err != nil {
		return rep.Fail(err, "添加失败"), err
	}
	return rep.Success(nil), nil
}

// Del
// @Description :
// @Param args :
// [Post] => /api/configuration/func/del
func (manage *FuncManagement) Del(args map[string]any) (rep.Response, error) {
	var err error
	// 检查 功能是否被分配到角色
	var ids []int
	if ids, err = manage.IsAuth(args); err != nil {
		return rep.Fail(err, "删除失败"), err
	}
	if ids != nil && len(ids) > 0 {
		err = errors.New("功能再用")
		return rep.Fail(err, "删除失败"), err
	}
	if err = manage.DelFunc(args); err != nil {
		return rep.Fail(err, "删除失败"), err
	}
	return rep.Success(nil), nil
}

// UpdateStatus
// @Description :
// @Param args :
// [Post] => /api/configuration/func/update/status
func (manage *FuncManagement) UpdateStatus(args map[string]any) (rep.Response, error) {
	if err := manage.ModifyStatus(args); err != nil {
		return rep.Fail(err, "修改失败"), err
	}
	return rep.Success(true), nil
}
